﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using Microsoft.ApplicationBlocks.Data;
using PickListControl;
namespace AnyOffice.general.system.workflow.flow_type
{ 
    public partial class type_setmanage : ModulePage
    {       
        private void LoadDeptNode(SqlConnection conn, int deptId, ref string deptName)
        {
            string commandText = "SELECT [ParentDept],[DeptName] FROM [dbo].[Department] WHERE [DeptId] = " + deptId.ToString();
            DataTable table = SqlHelper.ExecuteDataset(conn,CommandType.Text  , commandText).Tables[0];
            if (table.Rows.Count > 0)
            {
                deptName = table.Rows[0]["DeptName"].ToString() + "/" + deptName;
                if (Convert.ToInt32(table.Rows[0]["ParentDept"].ToString()) != 0)
                {
                    this.LoadDeptNode(conn, Convert.ToInt32(table.Rows[0]["ParentDept"].ToString()), ref deptName);
                }
            }
        }
   
        private string LoadNode(SqlConnection conn, int parentDept, string deptName)
        {
            if (parentDept != 0)
            {
                this.LoadDeptNode(conn, parentDept, ref deptName);
            }
            return deptName;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            int num;
            string str = "";
            if (!this.Page.IsPostBack)
            {
                num = (Request.QueryString["flow_id"] == null) ? 0 : int.Parse(Request.QueryString["flow_id"].ToString());
                this.ViewState["FlowId"] = num;
                DataTable table = new FlowTypeDao().SelectByFlowId(base.Conn, num);
                if (table.Rows.Count > 0)
                {
                    this.ViewState["SortId"] = table.Rows[0]["FlowSort"].ToString();
                    this.litFlowName.Text = table.Rows[0]["FlowName"].ToString();
                    str = (table.Rows[0]["ManageUser"] != DBNull.Value) ? table.Rows[0]["ManageUser"].ToString() : "";
                    if (str != string.Empty)
                    {
                        str = "," + str + ",";
                    }
                }
                else
                {
                    return;
                }
                DataTable table2 = new UsersDao().SelectBaseAll(base.Conn);
                if (table2.Rows.Count > 0)
                {
                    for (int i = 0; i < table2.Rows.Count; i++)
                    {
                        ListItem item = new ListItem("[" + this.LoadNode(base.Conn, Convert.ToInt32(table2.Rows[i]["ParentDept"].ToString()), table2.Rows[i]["DeptName"].ToString()) + "] " + table2.Rows[i]["UserName"].ToString(), table2.Rows[i]["UserId"].ToString());
                        if (str.IndexOf("," + table2.Rows[i]["UserId"].ToString() + ",") >= 0)
                        {
                            this.pklManageUser.ItemsLeft.Add(item);
                        }
                        else
                        {
                            this.pklManageUser.ItemsRight.Add(item);
                        }
                    }
                }
            }
            if (this.Page.IsPostBack)
            {
                num = Convert.ToInt32(this.ViewState["FlowId"]);
                foreach (ListItem item2 in this.pklManageUser.ItemsLeft)
                {
                    str = str + item2.Value + ",";
                }
                new FlowTypeDao().UpdateManageUser(base.Conn, num, str);
                base.CloseConn();
                Response.Redirect("type_list.aspx?sort_id=" + this.ViewState["SortId"].ToString());
            }
        }

        
    }
}

